home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / TEACH_C.ZIP / TEST6-8 < prev    next >
Text File  |  1986-04-04  |  5KB  |  149 lines

  1. .T
  2.    and now a TEST   
  3. .R4C1
  4.     If we declare ~b~Ix~N to be an ARRAY of ~I6~N rows and ~I7~N columns, then:
  5. .Q
  6. The member of the ARRAY in row 3, column 3 is called 
  7. x[2][2]
  8.     ~Inot~N x(2,2) or x(2)(2) or x[3,3) or etc. etc.
  9.  
  10. .Q
  11. Which member does x+5*7+2 select ?
  12. x[5][2]
  13.  
  14. .Q
  15. Where does x point to ?
  16. x is NOT a pointer!
  17.  
  18.     ( The names of ~I1~N-dimensional arrays are ~w~Rpointers~N.)
  19.     ( Since, for example, ~b~Ix[2]~N is a 1-dim array, ~IIT~N is a ~w~Rpointer~N.)
  20. .Q
  21. x[3] points to which element ? 
  22. x[3][0]
  23. .WN
  24. 0 ~b~I#include <stdio.h>                      ~N
  25. 1 ~b~Imain() {                                ~N
  26. 2 ~b~I    char c;                             ~N
  27. 3 ~b~I    while ( (c=getchar()) !='e' ) {     ~N
  28. 4 ~b~I        if ( 'a'<=c && c<='z')          ~N
  29. 5 ~b~I            printf("%c",c+'A'-'a');     ~N
  30. 6 ~b~I    }                                   ~N
  31. 7 ~b~I}                                       ~N
  32. .Q
  33. If you type the letter  p  what will the program print ? 
  34. P
  35. .Q
  36. If you type the letter  P  what will the program print ? 
  37. P
  38. .Q
  39. What must you type to have the program stop ?
  40. e
  41. .WR9C1
  42.                                                                            
  43.                                                                            
  44.                                                                            
  45.                                                                            
  46.                                                                            
  47.                                                                            
  48.                                                                            
  49.                                                                            
  50.                                                                            
  51.                                                                            
  52. .w
  53. .R10C1
  54.     In Line 3, the program will ~b~Igetchar~N from the keyboard and assign it
  55.     to ~b~Ic~N. As long as ~b~Ic~N is NOT EQUAL to 'e' it enters the ~b~Iwhile~N loop.
  56.  
  57.     In Line 4 it checks if ~b~Ic~N is a lower case 'a' to 'z'. If so, Line 5
  58.     is executed.
  59.  
  60.     Line 5 will print the ~b~I%c~Nharacter whose 'value' is:
  61.     ~Ithe value of c + the value of 'A' - the value of 'a'~N
  62.  
  63.     If you type a ~b~Ip~N, the ASCII value is ~I112~N.
  64.     The ASCII value of 'A' is ~I65~N and for 'a' it's ~I97~N.
  65.     The value of ~b~Ic+'A'-'a'~N is ~I112+65-97~N which is ~I80~N, the ASCII
  66.     value for P ...so the program types a P ...and, in general, it converts
  67.     all lower case letters to upper case!
  68. .WN
  69. 0 ~b~I#include <stdio.h>                      ~N
  70. 1 ~b~Imain(A,B)  {                            ~N
  71. 2 ~b~Ichar B                                  ~N
  72. 3 ~b~Iint A;                                  ~N
  73. 4 ~b~Iprintf("%s",b[1]);                      ~N
  74. 5 ~b~I}                                       ~N
  75.  
  76. .Q
  77. How many errors ? 
  78. TOO MANY!
  79.     The arguments of ~b~Imain()~M MUST be declared ~Ibefore~N the opening ~b~I{~N.
  80.     The first argument is ALWAYS an ~b~Iint~N.
  81.     The second argument is ALWAYS a ~w~Rpointer~N to an ARRAY (of ~w~Rpointers~N).
  82.     Their IS a difference between B[1] and b[1] (Line 4).
  83.     Lastly (firstly?), Line 2 needs the  !@#$%  SEMI-COLON!
  84. .W
  85. 0 ~b~I#include <stdio.h>                      ~N
  86. 1 ~b~Imain(A,B)                               ~N
  87. 2 ~b~Iint A;                                  ~N
  88. 3 ~b~Ichar *B[];                              ~N
  89. 4 ~b~I{                                       ~N
  90. 5 ~b~Iprintf("%s",B[1]);                      ~N
  91. 6 ~b~I}                                       ~N
  92. .WN
  93. 0 ~b~I#include <stdio.h>                      ~N
  94. 1 ~b~Imain(argc,argv)                         ~N
  95. 2 ~b~Iint argc;                               ~N
  96. 3 ~b~Ichar *argv[];                           ~N
  97. 4 ~b~I{                                       ~N
  98. 5 ~b~I    FILE *file_pointer, *fopen();       ~N
  99. 6 ~b~I    file_pointer=fopen(argv[1],"r");    ~N
  100. 7 ~b~I}                                       ~N
  101.  
  102.     If the above program is compiled/linked under the name ~Isam~N, then
  103.     executed with the command:
  104.  
  105. ~Isam george~N
  106. .Q
  107. what is the value of the integer argc ?
  108. 2
  109. .Q
  110. what string does argv[0] point to ? 
  111. sam
  112. .Q
  113. what string does argv[1] point to ? 
  114. george
  115. .WN
  116. 0 ~b~I#include <stdio.h>                      ~N
  117. 1 ~b~Imain() {                                ~N
  118. 2 ~b~I    char x[5]={'1','2','3','4','5'};    ~N
  119. 3 ~b~I    char *y;                            ~N
  120. 4 ~b~I    y=x;                                ~N
  121. 5 ~b~I    printf("%s",x);                     ~N
  122. 6 ~b~I    printf("%s",y);                     ~N
  123. 7 ~b~I}                                       ~N
  124. .Q
  125. What does Line 5 print ? 
  126. 12345Φ w
  127.     Since we forgot to terminate ~b~Ix[]~N with ~I'\0'~N, then ~b~Iprintf()~N
  128.     will start printing the characters, beginning with the ~Iaddress~N determined
  129.     by the ~r~Ipointer~N ~b~Ix~N, and continue  ...and continue... until (finally)
  130.     it reaches a ~I0~N (somewhere in memory!) ...yielding lots of garbage!
  131. .Q
  132. What does Line 6 print ? 
  133. 12345Φ w
  134.  
  135.     In Line 4, the VARIABLE POINTER ~b~Iy~N is made to point to the same ~Istring~N
  136.     that ~b~Ix~N points to, so Line 6 prints the same 12345+garbage!
  137. .WN
  138.  
  139.  
  140. .T
  141.    That's all folks!   
  142.  
  143.  
  144. .K16,32
  145. au revoir!
  146.  
  147. .q
  148.  
  149.